package org.emergency.framework.model;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.emergency.framework.constant.ModelConstant;

@Entity
@Table(name = "TB_ROLE", catalog = ModelConstant.DB_CATALOG)
public class TbRole implements Serializable {
	private static final long serialVersionUID = -5760548693831789331L;

	@Id
	@Column(name = "TB_ROLE_ID", unique = true, nullable = false, length = ModelConstant.STRING_LENGHT_id)
	private String tbRoleId;

	@Column(name = "name", length = ModelConstant.STRING_LENGHT_1)
	private String name;

	@Column(name = "isEnabled")
	private boolean isEnabled;

	@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "tbRole")
	private Set<TbRolePermission> tbRolePermissions = new HashSet<TbRolePermission>(
			0);	//和角色权限的中间表

	@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "tbRole")
	private Set<TbUserRoleEntity> tbUserRoleEntities = new HashSet<TbUserRoleEntity>(
			0);

	/** default constructor */
	public TbRole() {
	}

	/** minimal constructor */
	public TbRole(String tbRoleId) {
		this.tbRoleId = tbRoleId;
	}

	/** full constructor */
	public TbRole(String tbRoleId, String name,
			Set<TbRolePermission> tbRolePermissions,
			Set<TbUserRoleEntity> tbUserRoleEntities) {
		this.tbRoleId = tbRoleId;
		this.name = name;
		this.tbRolePermissions = tbRolePermissions;
		this.tbUserRoleEntities = tbUserRoleEntities;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((tbRoleId == null) ? 0 : tbRoleId.hashCode());
		return result;
	}


	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		TbRole other = (TbRole) obj;
		if (tbRoleId == null) {
			if (other.tbRoleId != null)
				return false;
		} else if (!tbRoleId.equals(other.tbRoleId))
			return false;
		return true;
	}

	public String getTbRoleId() {
		return tbRoleId;
	}

	public void setTbRoleId(String tbRoleId) {
		this.tbRoleId = tbRoleId;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Set<TbRolePermission> getTbRolePermissions() {
		return tbRolePermissions;
	}

	public void setTbRolePermissions(Set<TbRolePermission> tbRolePermissions) {
		this.tbRolePermissions = tbRolePermissions;
	}

	public Set<TbUserRoleEntity> getTbUserRoleEntities() {
		return tbUserRoleEntities;
	}

	public void setTbUserRoleEntities(Set<TbUserRoleEntity> tbUserRoleEntities) {
		this.tbUserRoleEntities = tbUserRoleEntities;
	}

	public boolean isEnabled() {
		return isEnabled;
	}

	public void setEnabled(boolean isEnabled) {
		this.isEnabled = isEnabled;
	}

}
